using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;

namespace JobsTutorialSample.Step2
{
    [BurstCompile]
    public struct FindNearestJob : IJob
    {
        [ReadOnly] public NativeArray<float3> SeekerPositions; //只读
        [ReadOnly] public NativeArray<float3> TargetPositions; //只读

        public NativeArray<float3> NearestTargetPositions; //可读可写

        public void Execute()
        {
            for (int i = 0; i < SeekerPositions.Length; i++)
            {
                float3 pos = default;
                float nearestDisSq = float.MaxValue;
                foreach (var targetPos in TargetPositions)
                {
                    float disSq = math.distancesq(SeekerPositions[i], targetPos);
                    if (disSq < nearestDisSq)
                    {
                        nearestDisSq = disSq;
                        pos = targetPos;
                    }
                }

                NearestTargetPositions[i] = pos;
            }
        }
    }
}